package com.ftwj.demo.modular.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.message.impl.NoticeSend;
import com.ftwj.demo.modular.entity.NoticeBean;
import com.ftwj.demo.modular.service.NoticeService;
import com.ftwj.demo.modular.service.NoticeUserService;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* <p>Title: NoticeController</p>  
* <p>Description: 公告管理前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author HeYang
* @date 2020年4月10日 下午4:40:52
* @version V1.0
 */
@RestController 
@RequestMapping("/notice")
@Api(tags="公告管理接口", value="公告消息管理")
@Validated
public class NoticeController {

	@Autowired
	private NoticeService service;
	@Autowired
	private NoticeUserService userService;
	@Autowired
	private NoticeSend noticeSend;
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 公告分页列表</p>  
	 * @param current
	 * @param size
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="公告消息分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	public ResultBean<?> page(@RequestParam(required = true) Integer current, @RequestParam(required = true) Integer size,
			@RequestParam(name = "type",required = false) Integer type , @RequestParam(name = "title",required = false) String title) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,type,title);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 列表查询</p>  
	 * @param type
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("list")
	@ApiOperation(value="公告消息列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> list(@RequestParam(name = "type",required = false) Integer type , @RequestParam(name = "title",required = false) String title) {
		List<NoticeBean> list = service.getList(type,title);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 新增公告</p>  
	 * @param entity
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="新增公告", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated NoticeBean entity){
		noticeSend.send(entity);
		return ResultBean.success();
	}
	
	/**
	 * <p>Title: edit</p>  
	 * <p>Description: 修改公告</p>  
	 * @param entity
	 * @return 
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PutMapping("/update")
	@ApiOperation(value="修改公告", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> update(@RequestBody@Validated NoticeBean entity){
		noticeSend.send(entity);//必须存在公告id
		return ResultBean.success();
	}
	
	/**
	 * <p>Title: read</p>  
	 * <p>Description: 公告读取</p>  
	 * @param noticeId
	 * @return 
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PutMapping("/read")
	@ApiOperation(value="公告读取", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> read(@RequestParam(name="noticeId") Integer noticeId){
		if(userService.read(noticeId) >0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: dels</p>  
	 * <p>Description: 公告删除</p>  
	 * @param noticeIds
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@DeleteMapping("/dels")
	@ApiOperation(value="公告删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	public ResultBean<?> dels(@RequestBody Integer[] ids){
		Object[] keys=ids;
		service.remoByIds(keys);
		return ResultBean.success();
	}
	
	/**
	 * <p>Title: info</p>  
	 * <p>Description: 查看详情</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月29日  
	 * @version 1.0
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value="查看详情", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="公告id", dataType=DataType.STRING, paramType=ParamType.PATH)
	})
	public ResultBean<?> info(@NotNull@PathVariable Integer id) {
		NoticeBean notice = service.find(id);
		if(notice != null) {
			return ResultBean.success(notice);
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
}
